<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-03-21
 * Time: 17:32
 */
namespace app\common\controller;

use app\common\controller\Base;
use weixinhack\WxBase;

class Wxapibase extends Base {
    public $wxtoken;
    public $temp;
    public $wxuser;
    public $cxbsuser;
    public $cxbsuserauth;

    public function _initialize(){
        parent::_initialize();
        //  检测微信状态
        $wxbase = new WxBase();
        if(!session('wxuserdb.openid')){
            if(!request()->param('code')){
                $this->get_wxcode(request()->url(true));
            }
            $this->get_user(request()->param());
        }
        $this->wxuser = session('wxuserdb');
        if(!is_array($this->wxuser['conf'])){
            $this->wxuser['conf'] = unserialize($this->wxuser['conf']);
        }

        //  获取token
        $this->wxtoken = $wxbase->access_token($this->webdb['wxappid'],$this->webdb['wxappsecret']);

        if(!session('userdb.uid')){
            $this->cxbsuser = $this->get_cxbsuser($this->wxuser);
        }else{
            $this->cxbsuser = session('userdb');
        }
        $this->temp = config('template.view_path');
        $temps['head'] = $this->temp.'head.htm';
        $temps['foot'] = $this->temp.'foot.htm';
        //  获取编辑权限
        $this->cxbsuserauth = $this->cxbsuserauth();
        //  获取导航
        $this->menu();
        $this->assign([
            'temps' => $temps,
            'webdb' => $this->webdb,
            'wxuserdata' => $this->wxuser,
            'cxbsuser' => $this->cxbsuser,
            'bodyondblclick' => $this->cxbsuserauth,
        ]);
    }
    //  获取导航
    public function menu(){
        $navs = model('Nav')->where('seetype','5')->where('status','1')->order('sort desc,id asc')->select();
        if(empty($navs)){
            return true;
        }
        foreach ($navs as $k => $v){
            $v['icon'] = unserialize($v['icon']);
            $navs[$k] = $v;
        }
        $this->assign('wxnav',$navs);
        return true;
    }
    /**
     * @param $re_url 要返回的地址
     */
    public function get_wxcode($re_url){
        //先获取到这三个参数
        $wxbase = new WxBase();
        $wxcode = $wxbase->get_code($this->webdb['wxappid'],$re_url);
        return $this->redirect($wxcode);
    }
    //  写入用户数据
    public function get_user($getdata){
        $wxbase = new WxBase();
        $usertoken = $wxbase->get_user_token($this->webdb['wxappid'],$this->webdb['wxappsecret'],$getdata['code']);
        $userdata = model('UserApi')->where('openid',$usertoken['openid'])->find();
        if(empty($userdata)){
            $t = false;
            if(!empty($getdata['tuijian'])){
                $t = $getdata['tuijian'];
            }
            $userdata = $wxbase->get_user_data($usertoken['access_token'],$usertoken['openid']);
            $userdata = model('UserApi')->wxapisave($userdata,$t);
        }
        session('wxuserdb',$userdata);
        return $userdata;
    }
    public function get_cxbsuser($data){
        $cxbsuser= model('UserData')->where('uid',$data['uid'])->find()->toArray();
        $cxbsuser['group_name'] = model('AuthGroup')->where('id',$cxbsuser['group_id'])->value('title');
        if($cxbsuser['status'] == '0'){
            $this->error("禁止访问");
        }
        session('userdb',$cxbsuser);
        return $cxbsuser;
    }
    //  获取微信二维码
    public function get_qrcode($data){
        $wxbase = new WxBase();
        $get_qrcode = $wxbase->get_shorturl($this->wxtoken,$data);
        $get_qrcode = json_decode($get_qrcode,true);
        if($get_qrcode['errmsg'] == 'ok'){
            return $get_qrcode['short_url'];
        }else{
            $this->error("微信服务器未响应，请重试！");
        }
    }
    //  获取编辑权限
    protected function cxbsuserauth(){
        $bodyondblclick = '';
        if(session('_admin_') && session('userdb') == session('_admin_')){
            $bodyondblclick = "class='cx-bodydbclick' data-type='bodyonclick'";
        }
        return $bodyondblclick;
    }

    /**
     * @param $wxurl    需要获取api的链接地址
     * @return array    返回jsapi参数
     */
    public function get_wxjsapi($wxurl){
        $wxbase = new WxBase();
        $wxjsapi = $wxbase->get_jsapi($this->wxtoken);
        $wxjsapi = $wxbase->get_jssing($wxjsapi,$wxurl);
        return $wxjsapi;
    }
    //  获取微信用户收货地址

}